#!/usr/bin/python

import sys
import os
import re
import logging as log
from testUnit import tester 

class ntp():

    __Excute = None

    def __init__(self):
        self.__Excute=os.system
        return

    def operator(self,proxy,ips_worker,monitor,connector):
        nips = [proxy]
        if connector:
            nips.append(connector)
        for wip in ips_worker:
            nips.append(wip)
        if proxy != monitor:
            nips.append(monitor)
        for nip in nips:
            self.__Excute("ssh "+nip+" \"pkill -U ntp;yum install -y ntp;systemctl enable ntpd.service;systemctl restart ntpd.service\"")
            if nip == proxy:
                self.__Excute("scp  ./config/ntp/ntp-server.conf "+nip+":/etc/ntp.conf")
            else:
                self.__Excute("scp  ./config/ntp/ntp-client.conf "+nip+":/etc/ntp.conf")
            self.__Excute("ssh "+nip+" \"systemctl enable ntpd.service;systemctl restart ntpd.service\"")
        return            

    def install(self,ips,monitor=None):
        self.__Excute("pkill -U ntp")
	self.__Excute("yum install -y ntp")
        for ip in ips:
            self.__Excute("ssh "+ip+" yum install -y ntp")
	for ip in ips:
	    self.__Excute("ssh "+ip+" systemctl stop ntpd.service")
        return

    def config(self):
        self.__Excute("cp -f ./config/ntp/ntp.conf /etc/")
        return

    def run(self):
        self.__Excute("systemctl restart ntpd.service")
        return

    def client(self,ips,monitor,connector):
        local_ip=[]
        reader = os.popen("ip addr | grep inet")
        data = reader.readlines()
        reader.close()
        for d in data:
            temp = re.search("inet (\d+\.\d+\.\d+\.\d+)/.*$",d)
            if temp and temp.group(1) != "127.0.0.1":
                local_ip.append(temp.group(1))
        for ip in ips:
#            self.__Excute("ssh "+ip+" systemctl restart ntpd.service ")
            self.__Excute("ssh "+ip+" 'echo \"0 */1 * * * root ntpdate "+local_ip[0]+"\">> /etc/crontab'")
        self.__Excute("ssh "+monitor+" 'echo \"0 */1 * * * root ntpdate "+local_ip[0]+"\">> /etc/crontab'")
        # if monitor != connector:
        #     self.__Excute("ssh "+connector+" 'echo \"0 */1 * * * root ntpdate "+local_ip[0]+"\">> /etc/crontab'")
        return
    
if __name__ == "__main__":
    test = tester()
    if not test.test():
        raise Exception("less arguments or illegal arguments.")
    ips = test.get_ips()
    ntper = ntp()
    ntper.install(ips,"172.22.29.179")
    ntper.config()
    ntper.run()
    ntper.client(ips,"172.22.29.179")
